import axios, { AxiosInstance, CreateAxiosDefaults } from "axios";
import {BaseUrl, TimeOut} from "./config/configSet";

// 创建 axios 请求实例
const serviceAxios: AxiosInstance = axios.create({
  baseURL: BaseUrl, // 基础请求地址
  timeout: TimeOut, // 请求超时设置
  withCredentials: false // 跨域请求是否需要携带 cookie
} as CreateAxiosDefaults);

//创建请求拦截
serviceAxios.interceptors.request.use(
  (config) => {
    config.headers["Authorization"] = `Bearer ${localStorage.getItem("token")}`; // 请求头携带 token
    // 设置请求头
    config.headers["Content-Type"] = "application/json;charset=utf-8"; // 默认类型
    config.headers['Access-Control-Allow-Origin'] = '*'
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
)
// 创建响应拦截
serviceAxios.interceptors.response.use(
  (res) => {
    // 处理自己的业务逻辑，比如判断 token 是否过期等等
    // 代码块
    // return res.data;
    switch (res.data.code) {
      case 0:
        return res.data.data;
      case 401:
        localStorage.clear()
        window.location.href = '/login'
        break
      default:
        console.error("接口报错了")
    }
  },
  (error) => {
    let message = "";
    if (error && error.response) {
      switch (error.response.status) {
        case 302:
          message = "接口重定向了！";
          break;
        case 400:
          message = "参数不正确！";
          break;
        case 401:
          message = "您未登录，或者登录已经超时，请先登录！";
          break;
        case 403:
          message = "您没有权限操作！";
          break;
        case 404:
          message = `请求地址出错: ${error.response.config.url}`;
          break;
        case 408:
          message = "请求超时！";
          break;
        case 409:
          message = "系统已存在相同数据！";
          break;
        case 500:
          message = "服务器内部错误！";
          break;
        case 501:
          message = "服务未实现！";
          break;
        case 502:
          message = "网关错误！";
          break;
        case 503:
          message = "服务不可用！";
          break;
        case 504:
          message = "服务暂时无法访问，请稍后再试！";
          break;
        case 505:
          message = "HTTP 版本不受支持！";
          break;
        default:
          message = "异常问题，请联系管理员！";
          break;
      }
    }
    return Promise.reject(message);
  }
);
export default serviceAxios;